package com.shengqi.xinnongren.work.ui.home.fragments.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class ShadowLinearLayout extends LinearLayout {

    private float mDensity;
    private Paint mPaint;
    private float mRadius;
    private RectF mRectF;
    private int mWidth;
    private int mHeight;

    public ShadowLinearLayout(Context context) {
        this(context, null);
    }

    public ShadowLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ShadowLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParams(context);
    }

    private void initParams(Context context) {
        setWillNotDraw(false);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mDensity = context.getResources().getDisplayMetrics().density;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.WHITE);
        mPaint.setShadowLayer(7 * mDensity, 0, 1 * mDensity, Color.parseColor("#10000000"));
        mRadius = 6 * mDensity;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        float v = 10 * mDensity;
        float f = 6 * mDensity;
        mRectF = new RectF(v, f, w - v, h - f);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
        mPaint.setColor(Color.parseColor("#E7E7E7"));
        mPaint.setStrokeWidth(1);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint);
    }
}
